package com.yby6.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yby6.common.core.controller.BaseController;
import com.yby6.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.yby6.common.core.AjaxResult;
import com.yby6.common.core.domain.SysUser;
import com.yby6.common.core.page.TableDataInfo;
import com.yby6.system.vo.SysUserVo;

/**
 * @program: yangbuyi-rbac
 * @ClassName: SysUserController
 * @create: 2021-05-15 19:05
 * @author: Yang Shuai
 * @since： JDK1.8
 * @SysUserController: 用户管理$
 **/


@RestController
@RequestMapping("/system/sysUser")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 分页用户列表
     *
     * @param sysUser
     * @return
     */
    @GetMapping("/list")
    @SaCheckPermission("sys:user:list")
    public TableDataInfo list(SysUser sysUser) {
        startPage();
        return getDataTable(this.sysUserService.list(sysUser));
    }

    /**
     * 根据ID查询用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    @SaCheckPermission("sys:user:getInfo")
    public AjaxResult getById(@PathVariable("id") Long id) {
        return AjaxResult.success().addParam(this.sysUserService.getUserById(id));
    }

    /**
     * 保存用户
     *
     * @return AjaxResult
     */
    @PostMapping("/save")
    @SaCheckPermission("sys:user:add")
    public AjaxResult add(@Validated @RequestBody SysUserVo sysUserVo) {
        sysUserVo.setType(0);
        return toAjax(this.sysUserService.insert(sysUserVo));
    }

    /**
     * 修改用户
     *
     * @return AjaxResult
     */
    @PutMapping("/update")
    @SaCheckPermission("sys:user:update")
    public AjaxResult update(@Validated @RequestBody SysUserVo sysUserVo) {
        return toAjax(this.sysUserService.update(sysUserVo));
    }

    /**
     * 批量删除用户
     *
     * @return AjaxResult
     */
    @DeleteMapping("/delete/{ids}")
    @SaCheckPermission("sys:user:delete")
    public AjaxResult delete(@PathVariable Long[] ids) {
        return toAjax(this.sysUserService.deleteUserByIds(ids));
    }

    /**
     * 禁用/启用用户
     *
     * @return AjaxResult
     */
    @PostMapping(value = {"/enabled/{id}/{status}"})
    @SaCheckPermission("sys:user:update")
    public AjaxResult enabled(@PathVariable Long id, @PathVariable Long status) {
        return toAjax(this.sysUserService.enabled(id, status));
    }
}
